#!/bin/bash
##一键配置主机映射
cat_init(){
	cat /etc/hosts
	if [[ "$hs" == "2" ]];then
		echo -e "继续追加自定义映射[y/n]\n默认N"
		read zdy
		if [[ "$zdy" == "y" || "$zdy" == "Y" ]];then
			zjs='1'
			while [ -z "$zjs" ];do
				echo '请输入IP'
				read ip
				echo '请输入域名/主机名'
				read ht
				echo -e "${ip} ${ht}" >> /etc/hosts
				echo -e "当前配置如下\n如需退出请输入任意字符,然后回车"
				cat /etc/hosts
				read zjs
			done
		else
			echo '完成配置'
		fi
		hs="1"
		re="q"
	else
		echo -e "当前映射如上,是否重置/追加[Y/N]\n默认N"
		read re
	fi
	if [[ "$re" == "y" || "$re" == "Y" ]];then
		h1=`cat /etc/hosts| awk "NR==1{print}"`
		h2=`cat /etc/hosts| awk "NR==2{print}"`
		echo -e "${h1}\n${h2}" > /etc/hosts
		echo -e "\n\n\n\n\n\n"
		info_set
	fi
}
info_set(){
	echo -e "请输入Master节点的数量"
	read M_s
	echo -e "请输入Node节点的数量"
	read N_s
	info_print
}
info_print(){
	ty="Master Node"
	hs="1"
	for n in $ty;do
		if [[ "$hs" == "1" ]];then
			SS="$M_s"
		elif [[ "$hs" == "2" ]];then
			SS="$N_s"
		fi
		for((i=1;i<="${SS}";i++));do
			echo -e "请输入${n}${i}的IP,本脚本不做IP格式检测，请认真填写"
			read ip
			echo -e "${ip} ${n}${i}" >> /etc/hosts
		done
		export hs="2"
	done
	cat_init
}
hs=1
cat_init
